package com.gitee.controller.user;

import com.gitee.entity.FollowList;
import com.gitee.result.Result;
import com.gitee.service.FollowListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/followListController")
@Api(tags = "关注列表接口")
public class FollowListController {

    @Resource
    private FollowListService followListService;

    @PostMapping("/follow/{followId}")
    @ApiOperation("关注用户")
    @CacheEvict(cacheNames = {"fansListCache", "followListCache"}, allEntries = true)
    public Result<?> follow(@PathVariable Integer followId) {
        log.info("关注用户: {}", followId);
        followListService.follow(followId);
        return Result.success();
    }

    @GetMapping("/getFollow/{userId}")
    @ApiOperation("获得用户关注列表")
    @Cacheable(cacheNames = "followListCache", key = "#userId")
    public Result<List<FollowList>> getFollow(@PathVariable Integer userId) {
        log.info("获得用户关注列表: {}", userId);
        return Result.success(followListService.getFollow(userId));
    }

    @GetMapping("/isFollow/{userId}")
    @ApiOperation("查找当前登录用户是否关注该用户")
    public Result<Boolean> isFollow(@PathVariable Integer userId) {
        log.info("查找当前登录用户是否关注该用户: {}", userId);
        return Result.success(followListService.isFollow(userId));
    }
}
